﻿using Microsoft.Web.Testing.Light;
using System.Collections.Generic;

namespace ASPNETVN.PORTAL.Modules.DOCUMENT.Components
{
    
    public partial class Spider
    {
        public static Components.Item GetDataDetail(string url)
        {
            Components.Item item = new Components.Item();
            // Load the response into an HtmlElement.
            HtmlElement rootElement = GetRootElement(url);
            // Find div maincontent
            HtmlElementFindParams divTabInfoParam = new HtmlElementFindParams();
            divTabInfoParam.TagName = "div";
            divTabInfoParam.Attributes.Add("id", "tab-info");
            if (rootElement != null)
            {
                HtmlElement divTabInfo = rootElement.ChildElements.Find(divTabInfoParam);
                // AttachmentUrl.
                try
                {
                    string attachmenUrl = "http://csdl.thutuchanhchinh.vn" + ((HtmlAnchorElement)divTabInfo.ChildElements[2].ChildElements[1].ChildElements[0]).CachedAttributes.HRef;
                }
                catch
                {

                }
            }
            return item;
        }

        public static IList<HtmlAnchorElement> GetData(string url, uint amount)
        {
            IList<HtmlAnchorElement> results = new List<HtmlAnchorElement>();
            int itemCount = 0;
            string currentUrl = url;
            while (true)
            {
                // Load the response into an HtmlElement.
                HtmlElement rootElement = GetRootElement(currentUrl);
                // Find div maincontent
                HtmlElementFindParams divMainContentParam = new HtmlElementFindParams();
                divMainContentParam.TagName = "table";
                divMainContentParam.Attributes.Add("class", "results");
                if (rootElement != null)
                {
                    HtmlTableElement tableMainContent = (HtmlTableElement)rootElement.ChildElements.Find(divMainContentParam);

                    foreach (HtmlTableRowElement row in tableMainContent.Rows)
                    {
                        //string summary = row.Cells[1].ChildElements[0].CachedInnerText;
                        HtmlAnchorElement link = (HtmlAnchorElement)row.Cells[1].ChildElements[0];
                        results.Add(link);
                        //this.GetDataDetail("http://csdl.thutuchanhchinh.vn" + link);
                        //string publisher = row.Cells[2].CachedInnerText;
                        //string category = string.Empty;
                        //if (row.Cells[3].ChildElements.Count > 0)
                        //{
                        //    category = row.Cells[3].ChildElements[0].CachedInnerText;
                        //}
                        itemCount++;
                        if (itemCount >= amount) return results;
                    }
                }

                try
                {
                    HtmlElementFindParams spanNextParam = new HtmlElementFindParams();
                    spanNextParam.TagName = "span";
                    spanNextParam.Attributes.Add("class", "next");

                    if (rootElement != null)
                    {
                        HtmlElement spanNext = rootElement.ChildElements.Find(spanNextParam);
                        HtmlAnchorElement anchorNext = spanNext.ChildElements[0] as HtmlAnchorElement;
                        if (anchorNext != null)
                        {
                            string nextLink = anchorNext.CachedAttributes.HRef;
                            currentUrl = "http://csdl.thutuchanhchinh.vn" + nextLink;
                        }
                    }
                }
                catch
                {
                    break;
                }
            }
            return results;
        }
    }
}
